<?php
namespace app\api\controller;

use app\api\logic\CartLogic;
use think\Request;
/**
 * 购物车
 * Class Cart
 * @package app\api\controller
 */
class CartController extends ApiBase
{

    protected $authExcept = [
        'cart_num'
    ];
    public function cart_num(Request $request)
    {
        $param = $request->param();
        $user_id =$this->uid??0;
        $type=$param['type']??1;
        $cart_num=0;
        if($user_id){
            $cart_num=CartLogic::cartNum($user_id);
        }
        return success($cart_num);
    }

    public function lists(Request $request)
    {
        $param = $request->param();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $cart_list=CartLogic::lists($user_id);
        return success($cart_list);
    }


    public function add()
    {
        $post = $this->request->post();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $check = $this->validate($post, 'app\api\validate\Cart.add');
        if (true !== $check) {
            return error($check);
        }
        $res = CartLogic::add($post['item_id'], $post['goods_num'], $user_id);
        if ($res === true) {
            return success('','加入成功');
        }
        return error($res);
    }


    public function change()
    {
        $post = $this->request->post();
        $check = $this->validate($post, 'app\api\validate\Cart.change');
        if ($check !== true) {
            return error($check);
        }
        $res = CartLogic::change($post['cart_id'], $post['goods_num']);
        if ($res === true) {
            return success();
        }
        return error($res);
    }


    public function del()
    {
        $post = $this->request->post();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $check = $this->validate($post, 'app\api\validate\Cart.del');
        if (true !== $check) {
            return error($check);
        }
        if (CartLogic::del($post['cart_id'], $user_id)) {
            return success('删除成功');
        }
        return error('删除失败');
    }


    public function selected()
    {
        $post = $this->request->post();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $check = $this->validate($post, 'app\api\validate\Cart.selected');
        if (true !== $check) {
            return error($check);
        }
        CartLogic::selected($post, $user_id);
        return success();
    }
}